﻿using System;
using System.Windows;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using WorkoutTracker.Model;

namespace WorkoutTracker.ViewModelNameSpace
{
    public class ViewModel
    {
        public ObservableCollection<Workout> Workouts { get; set; }


        public void GetWorkouts()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Count > 0)
            {
                GetSavedWorkouts();
            }
            else
            {
                GetDefaultWorkouts();
            }
        }


        public void GetDefaultWorkouts()
        {
            ObservableCollection<Workout> a = new ObservableCollection<Workout>();

            // Items to collect
            Workouts = a;
            // MessageBox.Show("Got Workouts from default");
        }


        public void GetSavedWorkouts()
        {
            ObservableCollection<Workout> a = new ObservableCollection<Workout>();

            foreach (Object o in IsolatedStorageSettings.ApplicationSettings.Values)
            {
                a.Add((Workout)o);
            }

            Workouts = a;
            //MessageBox.Show("Got Workouts from storage");
        }

        public void SaveWorkouts()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            foreach (Workout a in Workouts)
            {

                if (settings.Contains(a.Name))
                {
                    settings[a.Name] = a;
                }
                else
                {
                    settings.Add(a.Name, a.GetCopy());
                }
            }

            settings.Save();
            MessageBox.Show("Finished saving Workouts");
        }
    }
}
